package com.molyfun.parents.activity.store;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.store.adapter.ExpenseRecordAdapter;
import com.molyfun.parents.activity.store.view.ExpenseRecordCellView;
import com.molyfun.parents.activity.store.view.ExpenseRecordTopBar;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.ExpenseRecordListResponse;
import com.molyfun.parents.rest.bean.UserResponse;
import com.molyfun.parents.view.swipeRefreshLayout.SwipeRefreshLayout;

import java.lang.reflect.Field;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * 消费记录
 */

public class ExpenseRecordActivity extends BaseActivity implements View.OnClickListener, ExpenseRecordTopBar.ExpenseRecordTopBarClickListener,
        SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener, ExpenseRecordCellView.ExpenseRecordCallback {

    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.tel)
    TextView mTel;
    @InjectView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @InjectView(R.id.record_list)
    RecyclerView mRecyclerView;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;
    @InjectView(R.id.expense_record_top_bar)
    ExpenseRecordTopBar mExpenseRecordTopBar;

    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;
    private ExpenseRecordAdapter mExpenseRecordAdapter;

    private int mCurrentCategory = ExpenseRecordTopBar.ExpenseRecordTopChooseTab.BAR_TO_AFFIRM;


    public static final void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ExpenseRecordActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expense_record);
        ButterKnife.inject(this);
        getSupportActionBar().setTitle("");

        mExpenseRecordAdapter = new ExpenseRecordAdapter(this, this);

        mBack.setOnClickListener(this);
        mTel.setOnClickListener(this);

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);
        mRecyclerView.setAdapter(mExpenseRecordAdapter);

        try {
            Field f = null;
            f = SwipeRefreshLayout.class.getDeclaredField("mTouchSlop");
            f.setAccessible(true);
            f.set(mSwipeRefreshLayout, 150);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        mExpenseRecordTopBar.setExpenseRecordTopBarClickListener(this);

        loadData(true);

    }

    @Override
    public void onPayConfirm(String id) {
        showProgressDialog();
        Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().orderProductConsume(id);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                dismissProgressDialog();
                if(response != null && response.body() != null){
                    if(response.body().getCode() == 200){
                        toast("消费成功");
                    }else{
                        toast(response.body().getMsg());
                    }
                }else{
                    toast("消费失败");
                }
                loadData(true);
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                dismissProgressDialog();
                toast("消费失败");
            }
        });
    }

    @Override
    public void onRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setLoading(false);
        loadData(true);
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            loadData(false);
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
        }
    }

    @Override
    public void onTopBarClick(int chooseTab) {
        mCurrentCategory = chooseTab;
        loadData(true);
//        switch (chooseTab){
//            case ExpenseRecordTopBar.ExpenseRecordTopChooseTab.BAR_TO_AFFIRM:
//                break;
//            case ExpenseRecordTopBar.ExpenseRecordTopChooseTab.BAR_CONSUMED:
//                break;
//        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.base_title_left:
                finish();
                break;
            case R.id.tel:
                List<UserResponse.DepartBean> departBeenList = MFPApplication.getInstance().getUser().getDepartlist();
                if(departBeenList != null && departBeenList.size() > 0){
                    UserResponse.DepartBean departBean = departBeenList.get(0);
                    Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+departBean.getDeparttel()));
                    ExpenseRecordActivity.this.startActivity(intent);
                }
                break;
        }
    }

    private void loadData(final boolean isRefresh){
        if(isRefresh){
            mCurrentPage = RestClient.DEFAULT_PAGE;
        }
        showProgressDialog();
        Call<ExpenseRecordListResponse> call = MFPApplication.getInstance().getRestClient().getConsumeRecordList(mCurrentCategory == ExpenseRecordTopBar.ExpenseRecordTopChooseTab.BAR_TO_AFFIRM? "init" : "pass" ,mCurrentPage, RestClient.DEFAULT_PAGE_SIZE);
        call.enqueue(new Callback<ExpenseRecordListResponse>() {
            @Override
            public void onResponse(Call<ExpenseRecordListResponse> call, Response<ExpenseRecordListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    if(isRefresh){
                        if(response.body().getData().size() > 0){
                            mExpenseRecordAdapter.refreshData(response.body().getData());
                            mHasMoreData = true;
                            mEmptyLayout.setVisibility(View.GONE);
                        }else{
                            mHasMoreData = false;
                            mExpenseRecordAdapter.clearAll();
                            mEmptyLayout.setVisibility(View.VISIBLE);
                        }
                    }else{
                        if(response.body().getData().size() > 0){
                            mHasMoreData = true;
                        }else{
                            mHasMoreData = false;
                        }
                        mExpenseRecordAdapter.addAll(response.body().getData());
                    }

                }
            }

            @Override
            public void onFailure(Call<ExpenseRecordListResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });

    }

}
